const RouterHook = require("../module/routerModule")
const { addChargePile,updateChargePile,deleteChargePile,getChargePileList } = require("../service/adminChargingStations")
const { AssertUtils } = require("../util/utils")
const {query} = require("../util/dbutil");
const sqlWhere = require("../util/SqlWhere");
const router = RouterHook()

/**
 * @swagger
 * /admin/charging/stations/getList:
 *    get:
 *      tags:
 *      - 管理后台充电桩
 *      summary: 获取充电桩列表
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: id
 *          description: 充电桩id
 *          required: false
 *          type: number
 *        - name: name
 *          description: 充电桩名称 支持模糊查询
 *          required: false
 *          type: string
 *        - name: address
 *          description: 充电桩地址 支持模糊查询
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *          description: 返回充电桩列表
 *          schema:
 *            type: object
 *            properties:
 *              data:
 *                type: array
 *                default: [
 *                    {
 *                      "name":"哈哈",
 *                      "img": "http://xxx.jpg",
 *                      "address": "111",
 *                      "km": 10,
 *                      "kw": 11,
 *                      "voltage": 11,
 *                      "status": "空闲"
 *                   }
 *                ]
 *        401:
 *          description: 参数错误
 * */
router.get("/getList", async (ctx) => {
    const q = ctx.request.query
    const {data,result} = await getChargePileList(q)
    ctx.body = {
        code:200,
        data,
        total:result[0].count
    }
}).noAuth()

/**
 * @swagger
 * /admin/charging/stations/add:
 *    post:
 *      tags:
 *      - 管理后台充电桩
 *      summary: 新增充电桩
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: name
 *          description: 充电桩名称
 *          required: true
 *          type: string
 *        - name: address
 *          description: 充电桩地址
 *          required: true
 *          type: string
 *        - name: voltage
 *          description: 电压
 *          required: true
 *          type: string
 *        - name: status
 *          description: 空闲/运行中/报警
 *          required: true
 *          type: string
 *        - name: img
 *          description: 充电桩图片
 *          required: false
 *          type: string
 *        - name: km
 *          description: 充电桩距离
 *          required: true
 *          type: number
 *        - name: kw
 *          description: 充电桩功率
 *          required: true
 *          type: string
 *      responses:
 *        200:
 *          description: 返回信息
 *          schema:
 *            type: object
 *            properties:
 *              data:
 *                type: array
 *                default: [
 *                    {
 *                      "message":"添加成功",
 *                   }
 *                ]
 *        401:
 *          description: 参数错误
 * */
router.post("/add", async (ctx) => {
    const body = ctx.request.body
    await addChargePile(body)
    ctx.result.success('添加成功')
})
/**
 * @swagger
 * /admin/charging/stations/update:
 *    post:
 *      tags:
 *      - 管理后台充电桩
 *      summary: 修改充电桩列表
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: id
 *          description: 充电桩id
 *          required: required
 *          type: number
 *        - name: name
 *          description: 充电桩名称
 *          required: true
 *          type: string
 *        - name: address
 *          description: 充电桩地址
 *          required: false
 *          type: string
 *        - name: voltage
 *          description: 电压
 *          required: true
 *          type: string
 *        - name: status
 *          description: 空闲/运行中/报警
 *          required: false
 *          type: string
 *        - name: img
 *          description: 充电桩图片
 *          required: false
 *          type: string
 *        - name: km
 *          description: 充电桩距离
 *          required: false
 *          type: number
 *        - name: kw
 *          description: 充电桩功率
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *          description: 返回信息
 *          schema:
 *            type: object
 *            properties:
 *              data:
 *                type: array
 *                default: [
 *                    {
 *                      "message":"修改成功",
 *                   }
 *                ]
 *        401:
 *          description: 参数错误
 * */
router.post("/update", async (ctx) => {
    const body = ctx.request.body
    await updateChargePile(body)
    ctx.result.success('修改成功')
})
/**
 * @swagger
 * /admin/charging/stations/delete:
 *    post:
 *      tags:
 *      - 管理后台充电桩
 *      summary: 删除充电桩列表
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: id
 *          description: 充电桩id
 *          required: required
 *          type: number
 *      responses:
 *        200:
 *          description: 返回信息
 *          schema:
 *            type: object
 *            properties:
 *              data:
 *                type: array
 *                default: [
 *                    {
 *                      "message":"删除成功",
 *                   }
 *                ]
 *        401:
 *          description: 参数错误
 * */
router.post("/delete", async (ctx) => {
    const { id } = ctx.request.body
    AssertUtils.StringNotEmpty(id, "请输入id")
    await deleteChargePile(id)
    ctx.result.success('删除成功')
})


module.exports = router
